package com.example.demo.javaexcel.controller;

import com.example.demo.javaexcel.mapper.JavaExcelMapper;
import com.example.demo.javaexcel.utils.JavaExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author : 默苍离
 * @Create_Date : 2020/12/26/14:12
 * @Pack_name : com.example.demo.javaexcel.controller : JavaExcelController
 * *****************************************************
 * 注释:
 */
@RestController
@CrossOrigin
@RequestMapping("/mileage")
public class JavaExcelController {

    @Autowired
    private JavaExcelMapper mapper;


    @PostMapping("/upload")
    @ResponseBody
    public String upload(MultipartFile file) throws IOException, ParseException {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }
        InputStream inputStream = file.getInputStream();
        List<ArrayList> list = JavaExcel.xls(inputStream);

        for (int i = 0; i < list.size(); i++) {
            Date date = (Date) list.get(i).get(0);
            String no = (String) list.get(i).get(1);
            Integer NO = mapper.getOneByID(no);
            System.out.println(NO);
            System.out.println(no);
            System.out.println(i);
            Double mileage = (Double) list.get(i).get(2);

            System.out.println(mileage);
            mapper.updateMileage(NO, date, mileage);
        }
        return "success!!!";
    }

}
